package com.shengdou.as.slopsapptest.Welcom;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.shengdou.as.slopsapptest.Activity.LoginActivity;
import com.shengdou.as.slopsapptest.Activity.MainActivity;
import com.shengdou.as.slopsapptest.R;
import com.shengdou.as.slopsapptest.base.BaseActivity;
import com.shengdou.as.slopsapptest.utils.Constants;
import com.shengdou.as.slopsapptest.utils.DensityUtils;
import com.shengdou.as.slopsapptest.utils.LogUtil;
import com.shengdou.as.slopsapptest.utils.SharedPreferenceUtil;


public class WelcomeActivity extends BaseActivity {
    private TextView close;
    int time = 3;
    private String phone;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            if (time > 0) {
                time--;
                close.setText("跳过 0" + time);
                handler.sendEmptyMessageDelayed(0, 1000);
            } else {
                LogUtil.e("手机号码3", phone);
                Intent intent = new Intent();
                if (phone == null || phone.equals("")) {
                    intent.setClass(WelcomeActivity.this, LoginActivity.class);
                } else {
                    intent.setClass(WelcomeActivity.this, MainActivity.class);
                }
                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                finish();
            }
            if (msg.what == Constants.FACTORYADD) {

            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcom);
        init();
    }

    private void init() {
        phone = SharedPreferenceUtil.getPreferenceString(WelcomeActivity.this, "mobile");
        close = (TextView) findViewById(R.id.close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close.setEnabled(false);
                handler.removeMessages(0);
                LogUtil.e("手机号码1", phone);
                Intent intent = new Intent();
                if (phone == null || phone.equals("")) {
                    intent.setClass(WelcomeActivity.this, LoginActivity.class);
                } else {
                    intent.setClass(WelcomeActivity.this, MainActivity.class);
                }
                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                finish();
            }
        });


        // 跳转界面
        // 判断是否是第一次运行
        String code = DensityUtils.getVersionCode(this);
        boolean isFirstIn = code.equals(SharedPreferenceUtil.getPreferenceString(getApplicationContext(), "VersionCode")) ? false : true;

        new Thread() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        close.setVisibility(View.VISIBLE);
                        close.setText("跳过 03");
                        handler.sendEmptyMessageDelayed(0, 1000);
                    }
                });

            }
        }.start();
    }



    @Override
    public void onBackPressed() {
    }
}
